
//
//  TextPickerView.m
//  h5Test
//
//  Created by scjh on 15/6/26.
//  Copyright (c) 2015年 wangliang0. All rights reserved.
//

#import "TextPickerView.h"

@interface TextPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong)NSMutableArray *pickerShowArray;

@end

@implementation TextPickerView


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.frame = frame;
        [self UI];
    }
    return self;
}

- (void)UI {
    
    self.delegate = self;
    self.dataSource = self;
}

- (NSMutableArray *)pickerShowArray {
    
    if (!_pickerShowArray) {
        
        _pickerShowArray = [NSMutableArray array];
    }
    return _pickerShowArray;
}

- (void)setPickerArray:(NSArray *)pickerArray {

    _pickerArray = pickerArray;
    
    int num = 0;
    NSMutableArray *array = [self.pickerArray mutableCopy];
    
    NSString *key = @"name";
   
    while ([array isKindOfClass:[NSArray class]]) {
        
        num ++;
        NSMutableArray *newArr = [[NSMutableArray alloc] init];
        for (NSDictionary *dci in array) {
            
            [newArr addObject:dci[@"name"]];
        }
        [self.pickerShowArray addObject:newArr];
        
        if (num == 1) {
            key = @"city";
        }else {
            key = @"district";
        }
        array = array[0][key];
    }
    [self reloadAllComponents];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
   
    return self.pickerShowArray.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return [self.pickerShowArray[component] count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return self.pickerShowArray[component][row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
    NSInteger allNum = [pickerView numberOfComponents];
    
    if (component != allNum - 1) {
        [self.pickerShowArray removeAllObjects];
        
        NSMutableArray *array = [self.pickerArray mutableCopy];
        NSString *key = @"name";
        for (int i = 0; i < allNum; i ++) {
        
            if (i == 0) {
                key = @"city";
            }else if (i == 1){
                key = @"district";
            }else {
                key = @"name";
            }
            
            NSMutableArray *newArr = [[NSMutableArray alloc] init];
            
            if ([array isKindOfClass:[NSArray class]]) {
                
                for (NSDictionary *dci in array) {
                    
                    [newArr addObject:dci[@"name"]];
                }
            }else {
                
                NSDictionary *oneDic = (NSDictionary *)array;
                [newArr addObject:oneDic[@"name"]];
            }
           
            [self.pickerShowArray addObject:newArr];
            
            NSInteger selectNum = [self selectedRowInComponent:i];
            if ([array isKindOfClass:[NSArray class]]) {
                
                array = array[selectNum][key];
            }else {
                
                NSDictionary *oneDic = (NSDictionary *)array;
                array = oneDic[key];
            }
            
        }
    }
    
    if (component < allNum) {
        
        NSInteger num = allNum - 1 - component;
        for (int i = 0; i < num; i ++) {
            
            [self reloadComponent: allNum - 1 - i];
        }
    }
}

- (NSString *)selectedOfPickerView {
    
    NSInteger num = [self numberOfComponents];
    NSMutableString *pickerString = [NSMutableString string];
    for (int i = 0; i < num; i ++) {
        
        if (i != 0) {
            [pickerString appendString:@" "];
        }
        NSInteger row = [self selectedRowInComponent:i];
        [pickerString appendString:self.pickerShowArray[i][row]];
    }
    return pickerString;
}


@end
